package com.ch03.service;

import com.ch03.mapper.DormitoryMapper;
import com.ch03.pojo.Dormitory;
import com.ch03.utils.MyBatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/**
 * @ClassName DormitoryService
 * @Description TODO
 * @Author Kx
 * @Date 2025/4/23 15:37
 * @Version 1.0
 */
public class DormitoryService {
    /***
     * @author: kx
     * @date: 2025/4/23 16:28
     * @param dormitoryId : 宿舍id
     * @return com.ch03.pojo.Dormitory
     * @description: 根据id查询宿舍信息
    */
    public Dormitory getDormitoryById(int dormitoryId) {
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            return sqlSession.getMapper(DormitoryMapper.class).getDormitoryById(dormitoryId);
        }
    }

    /***
     * @author: kx
     * @date: 2025/4/23 16:29
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @description: 查询所有宿舍信息
    */
    public List<Dormitory> findAllDormitory() {
        // 获取sqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            return sqlSession.getMapper(DormitoryMapper.class).findAllDormitory();
        }
    }

    /**
     * @author: kx
     * @date: 2025/4/23 19:15
     * @param name : 姓名
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @description: 根据姓名查询宿舍信息
    */
    public Map<String, Object> getUserByName(String name) {
        // 获取sqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            return sqlSession.getMapper(DormitoryMapper.class).getDormitoryByName(name);
        }
    }

    /**
     * @author: kx
     * @date: 2025/4/23 19:21
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @description: 获取所有宿舍信息,每一条宿舍信息都是map对象
    */
    public List<Map<String,Object>> findAllDormitoryMap() {
        // 获取sqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            return sqlSession.getMapper(DormitoryMapper.class).findAllDormitoryMap();
        }
    }

    /**
     * @author: kx
     * @date: 2025/4/23 19:26
     * @return int : 宿舍数量
     * @description: 统计宿舍数量
    */
    public int countDormitory() {
        // 获取sqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            return sqlSession.getMapper(DormitoryMapper.class).countDormitory();
        }
    }

    /**
     * @author: kx
     * @date: 2025/4/23 19:31
     * @param name : 姓名
     * @return java.util.List<com.ch03.pojo.Dormitory>
     * @description: 根据姓名模糊查询宿舍信息
    */
    public List<Dormitory> likeDormitory(String name) {
        // 获取sqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            return sqlSession.getMapper(DormitoryMapper.class).likeDormitory(name);
        }
    }
}
